package com.hfut.manage.controller;


import com.hfut.manage.pojo.Result;
import com.hfut.manage.pojo.Teacher;
import com.hfut.manage.pojo.TeacherDTO;
import com.hfut.manage.pojo.TeacherQueryParam;
import com.hfut.manage.service.IEducationService;
import com.hfut.manage.service.IFamilyService;
import com.hfut.manage.service.ITeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zwh
 * @since 2025-06-03
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @GetMapping
    public Result list(TeacherQueryParam teacherQueryParam) {
        List<Teacher> teacherList = teacherService.lambdaQuery()
                .eq(teacherQueryParam.getDepartmentId() != null, Teacher::getDepartmentId, teacherQueryParam.getDepartmentId())
                .eq(teacherQueryParam.getPositionId() != null, Teacher::getPositionId, teacherQueryParam.getPositionId())
                .eq(teacherQueryParam.getTitleId() != null, Teacher::getTitleId, teacherQueryParam.getTitleId())
                .list();
        return Result.success(teacherList);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.success(teacher);
    }

    @PostMapping
    public Result save(@RequestBody TeacherDTO teacherDTO) {
        boolean success = teacherService.saveTeahcherDTO(teacherDTO);
        if (!success) {
            return Result.error(500, "新增教师数据失败");
        }
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result editTeacher(@PathVariable("id") Integer id, @RequestBody TeacherDTO teacherDTO) {
        boolean success = teacherService.updateTeacherDTO(id, teacherDTO);
        if (!success) {
            return Result.error(500, "修改教师数据失败");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Integer id) {
        teacherService.removeTeacher(id);
        return Result.success();
    }
}
